function insert_mime_links() {
    var titles = $('div.article h2');
    var link = titles.find('span a').attr('href');
    var re = new RegExp('http://www\.douban\.com/people/(\\w+)/event\?\\w*');
    var groups = link.match(re);

    if (groups) {
        var uid = groups[1];

        var url = chrome.extension.getURL('/pages/eventsmap/eventsmap.html') + '?uid=' + uid + '&action=';

        $(titles[0]).append("<span class='pl'>&nbsp;<a href='" + url + "participate' target='_blank'>活动地图</a></span>");
        $(titles[1]).append("<span class='pl'>&nbsp;<a href='" + url + "wish' target='_blank'>活动地图</a></span>");
    }
}

function insert_events_links() {
    var re = new RegExp('http://(\\w+).douban.com/events');

    var url;
    var parts = window.location.href.match(re);

    if (parts) {
        url = chrome.extension.getURL('/pages/eventsmap/eventsmap.html') + '?' + $.param({
            location: parts[1]
        })
    } else {
        re = new RegExp('http://(\\w+)\.douban\.com/events/([-\\w]+)/(\\w+)/*');
        var parts = window.location.href.match(re);

        url = chrome.extension.getURL('/pages/eventsmap/eventsmap.html') + '?' + $.param({
            location: parts[1],
            time: parts[2],
            type: parts[3]
        })
    }

    if (url) {
        var container = $('div#content div.aside');

        container.prepend("<div><a href='" + url + "' target='_blank'>" +
            "<img src='" + chrome.extension.getURL('/assets/img/gmap.jpg') + "' style='width: 32px;'>活动地图</a></div>");
    }
}

read_option('douban_event_map', function (k, v) {
    if (v) {
        if (window.location.href.endsWith('mine')) {
            insert_mime_links();
        } else {
            insert_events_links();
        }
    }
});
